﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Linq;

namespace BySoft.Server.Common.WebCommon
{
    public class WebCaching
    {
        public const string UserInfoSessionString = "UserInfo";

        #region WebContext
        public static HttpContext CurrentContext
        {
            get
            {
                return HttpContext.Current;
            }
        }

        public static HttpSessionState CurrentSession
        {
            get
            {
                return CurrentContext.Session;
            }
        }

        public static HttpCookieCollection CurrentCookie
        {
            get
            {
                return CurrentContext.Request.Cookies;
            }
        }

        public static HttpCookieCollection WriteCookie
        {
            get
            {
                return CurrentContext.Response.Cookies;
            }
        }

        public static System.Web.Caching.Cache CurrentCaching
        {
            get
            {
                return CurrentContext.Cache;
            }
        }
        #endregion

        #region Caching
        /// <summary>
        /// 填加缓存
        /// </summary>
        /// <param name="feature"></param>
        /// <param name="cacheKey"></param>
        public static void AddCache(Object feature, string cacheKey)
        {
            if (CurrentContext != null)
            {
                DateTime expiration = DateTime.Now.AddDays(1);
                CurrentCaching.Add(cacheKey, feature, null, expiration, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
            }
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        public static Object GetCache(string cacheKey)
        {
            Object result = null;
            if (CurrentCaching != null)
            {
                result = CurrentCaching[cacheKey];
            }
            return result;
        }

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        public static void RemoveCache(string cacheKey)
        {
            if (CurrentCaching != null)
                CurrentCaching.Remove(cacheKey);
        }

        /// <summary>
        /// 刷新缓存
        /// </summary>
        /// <param name="feature"></param>
        /// <param name="cacheKey"></param>
        public static void RefreshCache(Object feature, string cacheKey)
        {
            if (CurrentCaching != null)
            {
                RemoveCache(cacheKey);
                if (feature != null)
                {
                    AddCache(feature, cacheKey);
                }
            }
        }

        #endregion

        #region User Info
        /// <summary>
        /// 当前用户的IP地址
        /// </summary>
        public static string CurrentIP
        {
            get
            {
                return CurrentContext.Request.UserHostAddress;
            }
        }

        /// <summary>
        /// 当前用户信息
        /// </summary>
        public static object CurrentUser
        {
            get
            {
                return CurrentSession[UserInfoSessionString];
            }
            set
            {
                CurrentSession[UserInfoSessionString] = value;
            }
        }
        #endregion

        #region Caching Object
        #region 权限控制
        /// <summary>
        /// 缓存角色和一级模块的关系
        /// </summary>
        public const string ROOTMODULES = "ROOTMODULES";
        public static object RootModuleCaching
        {
            get
            {
                return GetCache(ROOTMODULES);
            }
            set
            {
                RefreshCache(value, ROOTMODULES);
            }
        }

        /// <summary>
        /// 缓存角色和二级模块的关系
        /// </summary>
        public const string SUBMODULES = "SUBMODULES";
        public static object SubModuleCaching
        {
            get
            {
                return GetCache(SUBMODULES);
            }
            set
            {
                RefreshCache(value, SUBMODULES);
            }
        }

        /// <summary>
        /// 缓存所有模块
        /// </summary>
        public const string MODULES = "MODULES";
        public static object ModuleCaching
        {
            get
            {
                return GetCache(MODULES);
            }
            set
            {
                RefreshCache(value, MODULES);
            }
        }

        /// <summary>
        /// 缓存所有模块
        /// </summary>
        public const string DATASETMODULES = "DATASETMODULES";
        public static object ModuleDataSetCaching
        {
            get
            {
                return GetCache(DATASETMODULES);
            }
            set
            {
                RefreshCache(value, DATASETMODULES);
            }
        }

        /// <summary>
        /// 缓存所有模块的按钮功能
        /// </summary>
        public const string OBJEITC = "OBJEITC";
        public static object ObjectCaching
        {
            get
            {
                return GetCache(OBJEITC);
            }
            set
            {
                RefreshCache(value, OBJEITC);
            }
        }

        /// <summary>
        /// 缓存所有数据权限
        /// </summary>
        public const string FUNCTIONS = "FUNCTIONS";
        public static object FunctionCaching
        {
            get
            {
                return GetCache(FUNCTIONS);
            }
            set
            {
                RefreshCache(value, FUNCTIONS);
            }
        }

        /// <summary>
        /// 缓存所有角色
        /// </summary>
        public const string ROLES = "ROLES";
        public static object RoleCaching
        {
            get
            {
                return GetCache(ROLES);
            }
            set
            {
                RefreshCache(value, ROLES);
            }
        }

        /// <summary>
        /// 缓存所有模块的按钮功能
        /// </summary>
        public const string OBJECTDATASET = "OBJECTDATASET";
        public static object ObjectDataSetCaching
        {
            get
            {
                return GetCache(OBJECTDATASET);
            }
            set
            {
                RefreshCache(value, OBJECTDATASET);
            }
        }

        /// <summary>
        /// 缓存全部模块
        /// </summary>
        public const string ALLMODULES = "ALLMODULES";
        public static object ALLMODULESCaching
        {
            get
            {
                return GetCache(ALLMODULES);
            }
            set
            {
                RefreshCache(value, ALLMODULES);
            }
        }
        #endregion


        #region PDA程序最新版本

        /// <summary>
        /// 缓存PDA最新版本号
        /// </summary>
        public const string CACHING_PDA_LATEST_VERSION_ENTITY = "PDALatestVersionEntity";
        static object _PDALatestVersionEntity = null;
        public static object PDALatestVersionEntity
        {
            get
            {
                return _PDALatestVersionEntity;
                //return GetCache(CACHING_PDA_LATEST_VERSION_ENTITY);
            }
            set
            {
                //RefreshCache(value, CACHING_PDA_LATEST_VERSION_ENTITY);
                _PDALatestVersionEntity = value;
            }
        }

        /// <summary>
        /// 缓存PDA最新版本压缩文件名
        /// </summary>
        public const string CACHING_PDA_LATEST_VERSION_ZIPFILE = "PDALatestVersionZipFile";
        static object _PDALatestVersionZipFile = null;
        public static object PDALatestVersionZipFile
        {
            get
            {
                return _PDALatestVersionZipFile;
                //return GetCache(CACHING_PDA_LATEST_VERSION_ZIPFILE);
            }
            set
            {
                //RefreshCache(value, CACHING_PDA_LATEST_VERSION_ZIPFILE);
                _PDALatestVersionZipFile = value;
            }
        }

        #endregion

        #region  数据字典
        public const string ALLPARAMETERTYPES = "ALLPARAMETERTYPES";
        /// <summary>
        ///  缓存参数类别
        /// </summary>
        public static object ALLPARAMETERTYPESCaching
        {
            get
            {
                return GetCache(ALLPARAMETERTYPES);
            }
            set
            {
                RefreshCache(value, ALLPARAMETERTYPES);
            }
        }

        /// <summary>
        ///  缓存所有参数
        /// </summary>
        public const string ALLPARAMETERS = "ALLPARAMETERS";
        public static object ALLPARAMETERSCaching
        {
            get
            {
                return GetCache(ALLPARAMETERS);
            }
            set
            {
                RefreshCache(value, ALLPARAMETERS);
            }
        }

        
        public const string ALLTREESDATA = "ALLTREESDATA";
        /// <summary>
        /// 缓存所有树状
        /// </summary>
        public static object ALLTREESDATACaching
        {
            get
            {
                return GetCache(ALLTREESDATA);
            }
            set
            {
                RefreshCache(value, ALLTREESDATA);
            }
        }

        public const string ALLNEWS = "ALLNEWS";
        /// <summary>
        /// 缓存新闻
        /// </summary>
        public static object ALLNEWSCaching
        {
            get
            {
                return GetCache(ALLNEWS);
            }
            set
            {
                RefreshCache(value, ALLNEWS);
            }
        }

        public const string ALLNOTICES = "ALLNOTICES";
        /// <summary>
        /// 缓存公告
        /// </summary>
        public static object ALLNOTICESCaching
        {
            get
            {
                return GetCache(ALLNOTICES);
            }
            set
            {
                RefreshCache(value, ALLNOTICES);
            }
        }

        public const string ALLINPUTDATA = "ALLINPUTDATA";
        /// <summary>
        /// 缓存导入
        /// </summary>
        public static object ALLINPUTDATACaching
        {
            get
            {
                return GetCache(ALLINPUTDATA);
            }
            set
            {
                RefreshCache(value, ALLINPUTDATA);
            }
        }
        #endregion

        public const string ALLPROVINCE = "ALLPROVINCE";
        /// <summary>
        /// 缓存所属省份
        /// </summary>
        public static object ALLPROVINCECaching
        {
            get
            {
                return GetCache(ALLPROVINCE);
            }
            set
            {
                RefreshCache(value, ALLPROVINCE);
            }
        }

        public const string ALLTOPMENU = "ALLTOPMENU";
        /// <summary>
        /// 缓存头部菜单
        /// </summary>
        public static object ALLTOPMENUCaching
        {
            get
            {
                return GetCache(ALLTOPMENU);
            }
            set
            {
                RefreshCache(value, ALLTOPMENU);
            }
        }


        public const string ALLOFFICEMENU = "ALLOFFICEMENU";
        /// <summary>
        /// 缓存科室专栏菜单
        /// </summary>
        public static object ALLOFFICEMENUCaching
        {
            get
            {
                return GetCache(ALLOFFICEMENU);
            }
            set
            {
                RefreshCache(value, ALLOFFICEMENU);
            }
        }

        #endregion
    }
}
